//
//  StandardsView.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/6/17.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "StandardsView.h"
#import "StandardViewCell.h"


//商品头
#define HEAD_HEIGHT HEIGHT/7
//tableView
#define TABLEVIEW_HEIGHT HEIGHT-HEIGHT/12- HEIGHT/7-HEIGHT/3
//确定
#define SUREVIEW_HEIGHT HEIGHT/12



@interface StandardsView()<UITableViewDelegate,UITableViewDataSource,StandardViewCellDelegate>
{
    UIView *_alphaView;
    UIView *_showView;
    //记录选中的大小
    NSString *_sizeStr;
    //记录选中的颜色
    NSString *_colorStr;
    //费率
    NSString *_feilvStr;
    //选中的口味
    NSString *_tasteStr;
    //选中的套餐
    NSString *_taocanStr;
    //选中的型号
    NSString *_modelStr;
    //选中的规格
    NSString *_guigeStr;
    //选中的样式
    NSString *_ysContentStr;
    //
    BOOL _judgeNull;
}

@property(nonatomic,strong)UITableView *StandardView;

@property (nonatomic,strong) UILabel *numberLabel;

@property(nonatomic,strong)UIButton *reduceBtn;

@property(nonatomic,strong)NSMutableArray *standardsArray;

@property(nonatomic,strong)NSMutableArray *standardsNameArray;



@end

@implementation StandardsView

@synthesize buyNum = _buyNum;

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        self.buyNum = 1;
        //Ui搭建
        [self addStandardsHeaderView];
        [self addStandardsTableView];
        
    }
    return self;
}



- (UIView *)windowView
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    return window.subviews[0];
}

- (void)showStandardsView
{
    
    [UIView animateWithDuration:0.35 animations:^{
        _alphaView.alpha = 0.35;
        _showView.frame = CGRectMake(0,HEIGHT/3,WIDTH,HEIGHT/3*2);
    } completion:nil];
    
    [[self windowView] addSubview:self];
}

- (void)dismissStandardsView
{
    [UIView animateWithDuration:0.35 animations:^{
        _alphaView.alpha = 0;
        _showView.frame = CGRectMake(0,HEIGHT,WIDTH,HEIGHT/3*2);
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
    
}


#pragma mark >>>>>>>>取消
- (void)StandardsViewCancleBtnDidClick:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(StandardsViewCancleBtnDidMiss:)]) {
        [self.delegate StandardsViewCancleBtnDidMiss:button];
    }
}


#pragma mark >>>>>>>>确定
- (void)StandardsViewConfirmDidClick:(UIButton *)button
{
    
    NSLog(@"_sizeStr:%@",_sizeStr);
    NSLog(@"_colorStr:%@",_colorStr);
    NSLog(@"_feilvStr:%@",_feilvStr);
    NSLog(@"_guigeStr:%@",_guigeStr);
    NSLog(@"_modelStr:%@",_modelStr);
    NSLog(@"_taocanStr:%@",_taocanStr);
    NSLog(@"_tasteStr:%@",_tasteStr);
    
 

// 判断商品各个规格为空 提示View
    if ([_standardsArray containsObject:_sizeArray]) {
        if (_sizeStr == nil) {
            [self judgeShowView];
            return;
        }
    }
    if ([_standardsArray containsObject:_colorArray]) {
        if (_colorStr == nil) {
            [self judgeShowView];
            return;
        }
    }
    if ([_standardsArray containsObject:_feilvArray]) {
        if (_feilvStr == nil) {
            [self judgeShowView];
            return;
        }
    }
    if ([_standardsArray containsObject:_tasteArray]) {
        if (_tasteStr == nil) {
            [self judgeShowView];
            return;
        }
    }
    if ([_standardsArray containsObject:_taoCanArray]) {
        if (_taocanStr == nil) {
            [self judgeShowView];
            return;
        }
    }
    if ([_standardsArray containsObject:_modelArray]) {
        if (_modelStr == nil) {
            [self judgeShowView];
            return;
        }
    }
    if ([_standardsArray containsObject:_guigeArray]) {
        if (_guigeStr == nil) {
            [self judgeShowView];
            return;
        }
    }
    
    if ([_standardsArray containsObject:_ysContentArray]) {
        if (_ysContentStr == nil) {
            [self judgeShowView];
            return;
        }
    }
   
    if ([self.delegate respondsToSelector:@selector(StandardsViewConfirmBtnClick:sizeStr:colorStr:num:tasteStr:taocanStr:modelStr:guigeStr:feilvStr:ysContentStr:)]) {
        
        [self.delegate StandardsViewConfirmBtnClick:button sizeStr:_sizeStr colorStr:_colorStr num:[NSString stringWithFormat:@"%zd",self.buyNum] tasteStr:_tasteStr taocanStr:_taocanStr modelStr:_modelStr guigeStr:_guigeStr feilvStr:_feilvStr ysContentStr:_ysContentStr];
    }

    
}


//规格完整提示
- (void)judgeShowView
{
    _judgeNull = YES;
    
    MBProgressHUD *hud = [MBProgressHUD showMessage:@"请选择商品属性" toView:self dimBackground:NO AndCompleteBlock:nil];
    [hud show:YES];
    
}

#pragma mark >>>>>>>>弹出的商品规格选择
- (void)StandardViewCellDelegateClick:(NSString *)selectStr
{
    //是否包含
    if ([_sizeArray containsObject:selectStr]) {
        //大小
        _sizeStr = selectStr;
        
    }else if([_colorArray containsObject:selectStr] ){
        //颜色
        _colorStr = selectStr;
        
    }else if ([_tasteArray containsObject:selectStr]){
        //口味
        _tasteStr = selectStr;
        
    }else if ([_taoCanArray containsObject:selectStr]){
        //套餐
        _taocanStr = selectStr;
        
    }else if([_guigeArray containsObject:selectStr]){
        //规格
        _guigeStr = selectStr;
        
    }else if([_ysContentArray containsObject:selectStr]){
       
        //样式
        _ysContentStr = selectStr;
        NSInteger index =  [self.ysContentArray  indexOfObject:selectStr];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"StandardsViewContentNotification" object:nil userInfo:@{@"index":@(index)}];
        
        
    }else if ([_modelArray containsObject:selectStr]){
        //型号
        _modelStr = selectStr;
   
    }else{
        
        //费率
        _feilvStr = selectStr;
    }
    
}


#pragma mark >>>>>>>>>>规格数量添加
//规格商品数量增加
- (void)plusBtnNumBtnClick:(UIButton *)sender
{
    self.buyNum = self.buyNum + 1;
    
    [self reduceSelectColor:_reduceBtn];
}

//规格商品数量减少
- (void)reduceBtnNumBtnClick:(UIButton *)sender
{
    if (self.buyNum <= 1) {
        
        sender.selected = NO;
        
    }else{
        
        sender.selected = YES;
        self.buyNum = self.buyNum -1;
        if (self.buyNum == 1) {
            sender.selected = NO;
        }
    }
    
}

- (void)reduceSelectColor:(UIButton *)sender
{
    sender.selected = YES;
}

#pragma mark >>>>>>>>>赋值
- (void)setBuyNum:(NSInteger)buyNum
{
    _buyNum = buyNum;
    _numberLabel.text = [NSString stringWithFormat:@"%zd",_buyNum];
}

- (NSInteger)buyNum
{
    _buyNum = [_numberLabel.text integerValue];
    return _buyNum;
}

- (void)setSizeArray:(NSArray *)sizeArray
{
    _sizeArray = sizeArray;
    [self.standardsArray addObject:_sizeArray];
    
    NSString *str = @"尺寸";
    [self.standardsNameArray addObject:str];
}

- (void)setColorArray:(NSArray *)colorArray
{
    _colorArray = colorArray;
    
    [self.standardsArray addObject:_colorArray];
    NSString *str = @"颜色";
    [self.standardsNameArray addObject:str];
}

- (void)setTasteArray:(NSArray *)tasteArray
{
    _tasteArray = tasteArray;
    [self.standardsArray addObject:_tasteArray];
    NSString *str = @"口味";
    [self.standardsNameArray addObject:str];
}

- (void)setTaoCanArray:(NSArray *)taoCanArray
{
    _taoCanArray = taoCanArray;
    [self.standardsArray addObject:_taoCanArray];
    NSString *str = @"套餐";
    [self.standardsNameArray addObject:str];
    
}

- (void)setModelArray:(NSArray *)modelArray
{
    _modelArray = modelArray;
    
    [self.standardsArray addObject:_modelArray];
    NSString *str = @"型号";
    [self.standardsNameArray addObject:str];
    
}

- (void)setGuigeArray:(NSArray *)guigeArray
{
    _guigeArray = guigeArray;
    [self.standardsArray addObject:_guigeArray];
    NSString *str = @"规格";
    [self.standardsNameArray addObject:str];
}

- (void)setYsContentArray:(NSArray *)ysContentArray
{
    _ysContentArray = ysContentArray;
    [self.standardsArray addObject:_ysContentArray];
    NSString *str = [NSString stringWithFormat:@"%@",self.ysNameArray[0]];
    [self.standardsNameArray addObject:str];
}

- (void)setFeilvArray:(NSArray *)feilvArray
{
    _feilvArray = feilvArray;
    [self.standardsArray addObject:_feilvArray];
    NSString *str = @"费率";
    [self.standardsNameArray addObject:str];
}


#pragma mark >>>>>>>>dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.standardsArray.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row < self.standardsArray.count) {
        static NSString *identifier = @"StandardViewIdentifier";
        StandardViewCell *cell= [tableView cellForRowAtIndexPath:indexPath];

        if (cell==nil) {
            
            cell=[[StandardViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.standardArray =  self.standardsArray[indexPath.row];
            cell.delegate = self;
            cell.nameLabel.text = self.standardsNameArray[indexPath.row];

            
        }
        
        
        return cell;
        
    }else{
        
        UITableViewCell  *cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, WIDTH,50)];
        cell.textLabel.text = @"购买数量";
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        CGFloat btnWidth = 20;
        CGFloat margin = 15;
        
        UIButton *plusBtn = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH - margin -btnWidth, 0, btnWidth, 30)];
        [cell addSubview:plusBtn];
        [plusBtn addTarget:self action:@selector(plusBtnNumBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        CGPoint tempPoint = plusBtn.center;
        tempPoint.y = cell.center.y;
        plusBtn.center = tempPoint;
        [plusBtn setImage:[UIImage imageNamed:@"cart_addBtn_highlight"] forState:UIControlStateNormal];
        
        _numberLabel.frame = CGRectMake(plusBtn.x-30, plusBtn.center.y -10,30,20);
        _numberLabel.textAlignment = NSTextAlignmentCenter;
        [cell addSubview:_numberLabel];
        
        self.reduceBtn = [[UIButton alloc] initWithFrame:CGRectMake(_numberLabel.frame.origin.x - btnWidth,plusBtn.center.y - plusBtn.frame.size.height/2 , plusBtn.frame.size.width, plusBtn.frame.size.height)];
        
        [_reduceBtn addTarget:self action:@selector(reduceBtnNumBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [_reduceBtn setImage:[UIImage imageNamed:@"cart_cutBtn_nomal"] forState:UIControlStateNormal];
        [_reduceBtn setImage:[UIImage imageNamed:@"cart_cutBtn_highlight"] forState:UIControlStateSelected];
        [cell addSubview:_reduceBtn];
        
        return cell;
    }
    
    

    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < self.standardsArray.count) {
        StandardViewCell *cell =  [[StandardViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StandardViewIdentifier"];
        return [cell heightForCell:self.standardsArray[indexPath.row]];
    }else{
        return 50;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}


#pragma mark >>>>>>>>>> UI
- (void)addStandardsHeaderView
{
    
    //半透明视图
    _alphaView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,WIDTH,HEIGHT)];
    _alphaView.backgroundColor = [UIColor blackColor];
    _alphaView.alpha = 0;
    [[self windowView] addSubview:_alphaView];
    //点击黑色背景 消失
    UIButton *disButton = [UIButton buttonWithType:UIButtonTypeCustom];
    disButton.frame = CGRectMake(0,0,WIDTH,HEIGHT/3);
    [self addSubview:disButton];
    [disButton addTarget:self action:@selector(StandardsViewCancleBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //规格总体
    _showView = [[UIView alloc]initWithFrame:CGRectMake(0,HEIGHT,WIDTH,HEIGHT/3*2)];
    _showView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_showView];
    
    //头部商品信息
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0,0,WIDTH,HEAD_HEIGHT)];
    [_showView addSubview:_headView];
    _headView.backgroundColor = [UIColor whiteColor];
    
    
    _goodsImg = [[UIImageView alloc] init];
    _goodsImg.layer.borderWidth = 0.5;
    _goodsImg.layer.borderColor = DEFAULT_COLOR.CGColor;
    [_headView addSubview:_goodsImg];
    
    //名字
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.font = [UIFont systemFontOfSize:13];
    _nameLabel.numberOfLines = 2;
    [_headView addSubview:_nameLabel];
    
    //价格
    _saleLabel = [[UILabel alloc] init];
    _saleLabel.font = [UIFont systemFontOfSize:16];
    _saleLabel.textColor = DEFAULT_COLOR;
    [_headView addSubview:_saleLabel];
    
    UILabel *chooseLabel = [[UILabel alloc] init];
    chooseLabel.font = [UIFont systemFontOfSize:12];
    chooseLabel.text = @"请选择尺码、款式";
    chooseLabel.textColor = [UIColor grayColor];
    [_headView addSubview:chooseLabel];
    
    //费率
    //    _feilvLabel = [[UILabel alloc] init];
    //    _feilvLabel.font = [UIFont systemFontOfSize:13];
    //    _feilvLabel.textColor = [UIColor grayColor];
    //    [_headView addSubview:_feilvLabel];
    
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancleBtn setImage:[UIImage imageNamed:@"cancle"] forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(StandardsViewCancleBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:cancleBtn];
    
    CGFloat StandMargin = 10;
    
    
    [_goodsImg mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_headView.mas_left).offset(StandMargin);
        make.width.height.mas_equalTo(_headView.Height-StandMargin*2);
        make.centerY.equalTo(_headView.mas_centerY);
    }];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_goodsImg.mas_right).offset(StandMargin);
        make.top.equalTo(_goodsImg.mas_top);
        make.width.mas_equalTo(WIDTH -_headView.Height-StandMargin-30);
        //        make.height.mas_equalTo(22);
    }];
    
    [_saleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel.mas_left);
        make.top.equalTo(_nameLabel.mas_bottom).offset(StandMargin);
        make.width.equalTo(_nameLabel.mas_width);
        make.height.mas_equalTo(15);
    }];
    
    [chooseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel.mas_left);
        make.bottom.equalTo(_goodsImg.mas_bottom);
        make.width.equalTo(_nameLabel.mas_width);
    }];
    
    //    [_feilvLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    //
    //        make.right.equalTo(_headView.mas_right).offset(-StandMargin);
    //        make.bottom.equalTo(_goodsImg.mas_bottom);
    //        make.width.mas_equalTo(40);
    //
    //    }];
    
    [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(_headView.mas_right).offset(-StandMargin);
        make.top.equalTo(_goodsImg.mas_top);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(15);
    }];
    
    _numberLabel = [[UILabel alloc] init];
    _numberLabel.text = [NSString stringWithFormat:@"%ld",_buyNum];
    _numberLabel.font = [UIFont systemFontOfSize:14];
    _numberLabel.textColor = [UIColor blackColor];
    _numberLabel.textAlignment = NSTextAlignmentCenter;
    
    
}


- (void)addStandardsTableView
{
    //黑线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(_headView.frame), WIDTH,1)];
    lineView.backgroundColor = BACKGROUND_COLOR;
    [_showView addSubview:lineView];
    
    UITableView *StandardView = [[UITableView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(lineView.frame),WIDTH,TABLEVIEW_HEIGHT) style:UITableViewStyleGrouped];
    StandardView.showsVerticalScrollIndicator = false;
    [_showView addSubview:StandardView];
    self.StandardView = StandardView;
    StandardView.backgroundColor = [UIColor whiteColor];
    StandardView.delegate = self;
    StandardView.dataSource = self;
    StandardView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    UIView *lastView = [[UIView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(StandardView.frame)+0.5, WIDTH,SUREVIEW_HEIGHT)];
    [_showView addSubview:lastView];
    lastView.backgroundColor = [UIColor whiteColor];
    
    
    UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(0,1, WIDTH,1)];
    lineView2.backgroundColor = [UIColor blackColor];
    lineView2.alpha = 0.1;
    [lastView addSubview:lineView2];
    
    UIButton *sureButton = [[UIButton alloc] init];
    sureButton.layer.cornerRadius = 5;
    sureButton.layer.masksToBounds = YES;
    sureButton.layer.borderWidth = 0.5;
    sureButton.layer.borderColor = DEFAULT_COLOR.CGColor;
    sureButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureButton setBackgroundColor:DEFAULT_COLOR];
    [lastView addSubview:sureButton];
    [sureButton addTarget:self action:@selector(StandardsViewConfirmDidClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(lastView.mas_centerY);
        make.centerX.equalTo(lastView.mas_centerX);
        make.width.mas_equalTo(WIDTH/3);
        make.height.mas_equalTo(lastView.Height/3*2);
    }];
}

- (NSMutableArray *)standardsArray
{
    if (_standardsArray == nil) {
        _standardsArray = [NSMutableArray array];
    }
    return _standardsArray;
}

- (NSMutableArray *)standardsNameArray
{
    if (_standardsNameArray == nil) {
        _standardsNameArray = [NSMutableArray array];
    }
    return _standardsNameArray;
}




@end
